package com.prs.crm.service.doc.impl;

import java.util.Collection;
import java.util.Date;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.doc.ShareUserDocDao;
import com.prs.crm.domain.doc.ShareUserDoc;
import com.prs.crm.domain.doc.UserDoc;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.doc.ShareUserDocService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class ShareUserDocServiceImpl extends BaseServiceImpl<ShareUserDoc,ShareUserDocDao> implements ShareUserDocService{
	private PersonService personService;
	@Override
	public void saveShareUserDoc(ShareUserDoc shareUserDoc, String sharePersons) {
		// TODO Auto-generated method stub
		this.getDao().deleteShareDoc(shareUserDoc.getUserDoc());
		Collection<Person> persons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(sharePersons.split(",")));
		for(Person person:persons) {
			ShareUserDoc share = new ShareUserDoc();
			share.setUserDoc(shareUserDoc.getUserDoc());
			share.setEndTime(shareUserDoc.getEndTime());
			share.setBeginTime(shareUserDoc.getBeginTime());
			share.setRecorder(AppContext.getAccountContext().currentUser());
			share.setRecordTime(new Date());
			share.setPerson(person);
			
			this.getDao().save(share);
		}
	}
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	@Override
	public Collection<ShareUserDoc> getShareDoc(UserDoc userDoc) {
		// TODO Auto-generated method stub
		return this.getDao().getShareDoc(userDoc);
	}

}
